from rest_framework.views import exception_handler
from .response import ApiResponse


def custom_exception_handler(exc, context):
    # 调用DRF默认异常处理
    response = exception_handler(exc, context)

    if response is not None:
        return ApiResponse(
            data=None,
            message=response.data.get('detail', '请求错误'),
            code=response.status_code
        )

    # 处理其他未捕获异常
    return ApiResponse(
        data=None,
        message=str(exc),
        code=500
    )